package com.zh.sys.service.impl;

import com.zh.util.MyMail;
import com.zh.util.MyUtil;
import com.zhang.entity.Employee;
import com.zh.sys.dao.EmployeeDao;
import com.zh.sys.service.EmployeeService;
import com.zh.util.Mytask;
import com.zhang.entity.Examine;
import com.zhang.entity.Feedback;
import com.zhang.entity.Retuntity;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * (Employee)表服务实现类
 *
 * @author makejava
 * @since 2021-06-24 15:32:24
 */
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {
    @Resource
    private EmployeeDao employeeDao;

    @Resource
    private Mytask mytask;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param eid 主键
     * @return 实例对象
     */
    @Override
    public Employee queryById(Long eid) {
        return this.employeeDao.queryById(eid);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Employee> queryAllByLimit(int offset, int limit) {
        return this.employeeDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<Employee> queryAll(Employee employee) {
        return employeeDao.queryAllUser(employee);
    }


    /**
     * 新增数据
     *
     * @param employee 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public void insert(Employee employee) {
        ValueOperations<String, Object> strRedis = redisTemplate.opsForValue();
        employee.setInTime(new Date());
        strRedis.set(employee.getName()+":"+employee.getEmall(),Calendar.getInstance().getTimeInMillis());
        Employee employee1 = new Employee();
        employee1.setDepartId(employee.getDepartId());
        List<Employee> employees = employeeDao.queryAllUser(employee1);
        employee1.setIsDard(2);
        List<Employee> employees1 = employeeDao.queryAllUser(employee1);
        mytask.sendEmail(employees,employees1.get(0).getEmall());
        this.employeeDao.insert(employee);
    }

    /**
     *
     * @param uid 用户id
     * @param tid 导师id
     * 分配导师
     */
    @Override
    public void updateUser(String uid,String tid) {
        Employee employee = employeeDao.queryById(Long.valueOf(uid));
        employee.setTid(Long.valueOf(tid));
        employeeDao.update(employee);
    }



    /**
     *
     * @param uid 用户id
     * 更换导师
     */
    @Override
    public Retuntity updateTeacher(String uid) {
        Employee employee = employeeDao.queryById(Long.valueOf(uid));
        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
        Long time = (Long)opsForValue.get(employee.getName() + ":" + employee.getEmall());
        Long day = MyUtil.getDay(time);
        if (day>=15){
            Retuntity.error(20001,"当前员工入职天数已经超过15天");
        }
        employee.setTid(0L);
        employee.setIsUpdate(0);
        employeeDao.update(employee);
        Employee employee1 = new Employee();
        employee1.setIsDel(0);
        employee1.setIsDard(2);
        employee1.setDepartId(employee.getDepartId());
        List<Employee> employees = employeeDao.queryAllUser(employee);
        mytask.sendEmail(employees,employees.get(0).getEmall());
        return  Retuntity.success("导师修改成功");
    }

    @Override
    public List<Employee> queryByAllId(List<String> employs) {
        return employeeDao.queryByAllId(employs);
    }


    /**
     * 修改数据
     *
     * @param employee 实例对象
     * @return 实例对象
     */
    @Override
    public Employee update(Employee employee) {

        this.employeeDao.update(employee);
        return this.queryById(employee.getEid());
    }

    /**
     * 通过主键删除数据
     *
     * @param eid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long eid) {
        return this.employeeDao.deleteById(eid) > 0;
    }

    @Override
    public List<Employee> queryTid(List<String> departIds) {
        return this.employeeDao.queryTid(departIds);
    }



    @Override
    public void sendEmail(Examine examine) {
        Employee employee1 = employeeDao.queryById(Long.valueOf(examine.getEid()));
        Employee employee = new Employee();
        employee.setDepartId(employee1.getDepartId());
        employee.setIsDel(0);
        employee.setIsOfficial(1);
        List<Employee> employees = employeeDao.queryAllUser(employee);
        List<String> list = new ArrayList<>();
        for (Employee employee2 : employees) {
            Integer isDard = employee2.getIsDard();
            if (isDard!=4&&isDard!=5){
                list.add(employee2.getEmall());
            }
        }
        try {
            MyMail.mailLogin(examine.getCon(),"961995531@qq.com",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void sendEmailFb(Feedback feedback) {
        Employee employee = employeeDao.queryById(Long.valueOf(feedback.getEid()));
        Employee employee2 = new Employee();
        employee2.setIsDel(0);
        employee2.setDepartId(employee.getDepartId());
        List<Employee> employees = employeeDao.queryAllUser(employee2);
        List<String> list  = new ArrayList<>();
        for (Employee employee1 : employees) {
            list.add(employee1.getEmall());
        }
        try {
            MyMail.mailLogin("<h1>新的反馈</h1><br>"+feedback.getText(),"961995531@qq.com",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Employee getEmPl(Employee employee) {
        return employeeDao.queryAllUser(employee).get(0);
    }
}
